package cn.topevery.garden.dal.logic;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.ProjSituationRecordDao;
import cn.topevery.garden.entity.dto.ProjMoneyDTO;
import cn.topevery.garden.entity.dto.ProjSituationRecordDTO;
import cn.topevery.garden.entity.po.ProjBasicInfo;
import cn.topevery.garden.entity.po.ProjFiles;
import cn.topevery.garden.entity.po.ProjSituationRecord;
import cn.topevery.garden.entity.query.ProjSituationRecordQuery;
import cn.topevery.garden.entity.vo.ProjFilesVO;
import cn.topevery.garden.entity.vo.ProjSituationRecordMoneyVO;
import cn.topevery.garden.entity.vo.ProjSituationRecordVO;
import cn.topevery.garden.framework.Database;
import cn.topevery.garden.framework.base.BaseLogic;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 项目前期情况记录表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Service
public class ProjSituationRecordLogic extends BaseLogic<ProjSituationRecord> {

    @Autowired
    ProjBasicInfoLogic projBasicInfoLogic;

    @Autowired
    ProjSituationRecordDao projSituationRecordDao;

    @Autowired
    ProjFilesLogic projFilesLogic;

    @Override
    public BaseEntityDao<ProjSituationRecord, Integer> getDao() {
        return projSituationRecordDao;
    }

    /**
     * 获取列表
     *
     * @param query
     */
    public DataPager<ProjSituationRecordVO> listPage(ProjSituationRecordQuery query) {
        return projSituationRecordDao.listPage(query);
    }

    /**
     * 新增/修改
     *
     * @param userId
     * @param dto
     */
    public void insertOrUpdate(String userId, ProjSituationRecordDTO dto) {
        Database.transactionScope(() -> {
            if (ObjectUtil.isEmpty(dto.getId())) {
                ProjSituationRecordVO situationRecordVO = getByProjId(dto.projId);
                if (ObjectUtil.isNotEmpty(situationRecordVO)) {
                    throw new RuntimeException("已存在对应的项目前期情况数据");
                }
                dto.setSituationId(UUID.randomUUID().toString());
                insert(userId, dto);
            } else {
                if (ObjectUtil.isEmpty(dto.getSituationId())) {
                    throw new RuntimeException("situationId不能为空");
                }
                update(userId, dto);
            }
            //  保存附件
            if (ObjectUtil.isNotEmpty(dto.getFiles())) {
                List<ProjFiles> files = dto.getFiles().stream().filter(i -> ObjectUtil.isEmpty(i.getId())).collect(Collectors.toList());
                files.stream().forEach(i -> {
                    i.setProjId(dto.getProjId());
                    i.setProjStageId(dto.getSituationId());
                });
                if (CollUtil.isNotEmpty(files)) {
                    projFilesLogic.insert(userId, files);
                }
            }
            //  删除附件
            if (ObjectUtil.isNotEmpty(dto.getDeleteFileIds())) {
                projFilesLogic.delete(userId, dto.getDeleteFileIds());
            }
        });
    }

    public boolean deleteBatchById(Integer[] ids) {
        if (ObjectUtil.isEmpty(ids)) {
            throw new RuntimeException("传入的Ids不能为空！");
        }
        SysUser user = UserRuntime.getSysUser();
        Database.transactionScope(() -> {
            // 根据Ids查询前期情况信息
            ProjSituationRecordQuery query = new ProjSituationRecordQuery();
            query.setIds(ids);
            List<ProjSituationRecordVO> recordList = this.projSituationRecordDao.list(query);
            if (CollUtil.isNotEmpty(recordList)) {
                String projId = recordList.get(0).getProjId();
                List<String> situationIds = recordList.stream().map(projSituationRecordVO -> projSituationRecordVO.getSituationId()).collect(Collectors.toList());
                // 根据阶段Ids删除附件信息

                projFilesLogic.updateFilesByProjStageIds(situationIds, projId, user.id.toString());
            }
            this.delete(user.id.toString(), ids);
        });
        return true;
    }

    /**
     * 根据projId获得详情
     *
     * @param projId
     * @return
     */
    public ProjSituationRecordVO getByProjId(String projId) {
        ProjSituationRecordVO projSituationRecordVO = null;
        ProjSituationRecord projSituationRecord = projSituationRecordDao.getByProjId(projId);
        if (ObjectUtil.isNotEmpty(projSituationRecord)) {
            projSituationRecordVO = new ProjSituationRecordVO();
            // 复制前期情况
            BeanUtils.copyProperties(projSituationRecord, projSituationRecordVO);

            // 查询项目基本信息
            ProjBasicInfo projBasicInfo = projBasicInfoLogic.getByProjId(projSituationRecordVO.getProjId());
            if (ObjectUtil.isNotEmpty(projBasicInfo)) {
                projSituationRecordVO.setProjBasicInfo(projBasicInfo);
            }

            // 查询附件
            List<ProjFilesVO> files = projFilesLogic.listByProjIdAndStageId(projSituationRecordVO.getProjId(), projSituationRecordVO.getSituationId());
            if (CollUtil.isNotEmpty(files)) {
                projSituationRecordVO.setFiles(files);
            }
        }
        return projSituationRecordVO;
    }

    public ProjSituationRecordMoneyVO getEstimateMoney(ProjMoneyDTO moneyDTO) {
        if (ObjectUtil.isEmpty(moneyDTO)) {
            throw new RuntimeException("传入的对象不能为空！");
        }
        return this.projSituationRecordDao.getEstimateMoney(moneyDTO);
    }
}
